package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import process.Searching;


/**
 * Servlet implementation class Controller
 */
public class Controller extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static String DELETE_JSP = "/Delete.jsp";
	private static String EDIT_JSP = "/Edit.jsp";
	private static String SHOWALL_JSP = "/ShowAll.jsp";

       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Controller() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String forward="";
		// Get a map of the request parameters
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
                //On récupère la liste des noms de paramètres
		Enumeration<String> e = request.getParameterNames();
		List<String> prog = new ArrayList<String>();
//**************** Les paramètres passées dans l'URL
Searching search = new Searching();
String var;
int domain=0,specialist=0,lieu_travail=0,salary=0,titre=0,experience=0;
 var = request.getParameter("num_star_domain");
if (!var.equals(""))  domain = Integer.parseInt(request.getParameter("num_star_domain"));
var = request.getParameter("num_star_specialist");
if (!var.equals(""))  specialist = Integer.parseInt(request.getParameter("num_star_specialist"));


var = request.getParameter("num_star_work");
if (!var.equals(""))  lieu_travail = Integer.parseInt(request.getParameter("num_star_work"));
var = request.getParameter("num_star_salary");
if (!var.equals(""))  salary = Integer.parseInt(request.getParameter("num_star_salary"));

var = request.getParameter("num_star_titre");
if (!var.equals(""))  titre = Integer.parseInt(request.getParameter("num_star_titre"));
var = request.getParameter("num_star_year_experience");
if (!var.equals(""))  experience = Integer.parseInt(request.getParameter("num_star_year_experience"));

LinkedList<LinkedList<String>> result = search.findJobs(request.getParameter("domain"), request.getParameter("specialist"), request.getParameter("place_work"),
		Double.parseDouble(request.getParameter("salary")), request.getParameter("titre"), request.getParameter("year_experience"),
		domain,specialist, lieu_travail,salary, titre, experience); 
// num_star_domain 3,7,5,0,1,2
// initialiser une session 
HttpSession session = request.getSession();
//fournir à utilisateur une 70% des plus grands scores(5 cietre)
	int i = 0;
	 Iterator it = result.iterator();	
	 while (it.hasNext() ) {
		 LinkedList<String> secondList = (LinkedList<String>)it.next();
		 // Un nouveau poste à afficher
		 Iterator itt = secondList.iterator();	
		 while (itt.hasNext() ) {
			 session.setAttribute("poste"+(i++), (String)itt.next());
		     
		 }
	 }
     session.setAttribute("indice", String.valueOf(i));
	//Maintenant, nous allons utiliser cet objet dans une autre servlet
			response.sendRedirect("SearchJob.jsp");	
//***************
		//On parcours cette liste
		while(e.hasMoreElements()){
			String key = e.nextElement();
			//System.out.println("key = "+key);
				out.println("<p><strong> "+key+"</strong></p>");			
		}
		@SuppressWarnings("unchecked")
		Map parameters = request.getParameterMap();
		if (parameters.containsKey("delete")){
			forward = DELETE_JSP;
		} else if (parameters.containsKey("edit")){
			forward = EDIT_JSP;
		} else {
			forward = SHOWALL_JSP;
		}
		RequestDispatcher view = request.getRequestDispatcher(forward);
		//view.forward(request, response);
		/*
		 * <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<table>
    <c:forEach items="${products}" var="product">
        <tr>
            <td>${product.name}</td>
            <td>${product.description}</td>
            <td>${product.price}</td>
        </tr>
    </c:forEach>
</table>
		 */
	
}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
